package com.example.ineedahelp;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import com.netmera.mobile.BasePush.PushChannel;
import com.netmera.mobile.NetmeraAndroidPush;
import com.netmera.mobile.NetmeraException;
import com.netmera.mobile.NetmeraGeoLocation;
import com.netmera.mobile.NetmeraPushDetail;
import com.netmera.mobile.NetmeraPushService;
import com.netmera.mobile.push.NetmeraGeofence;
import com.netmera.mobile.push.NetmeraGeofenceService;

public class HelpActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		try {
			NetmeraPushService.register(getApplicationContext(), HelpActivity.class, GlobalVariables.projectId);
		} catch (NetmeraException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setContentView(R.layout.activity_help);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.help, menu);
		return true;
	}
	
	public void getHelp(View view){

		LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    
		
		final LocationListener listener = new LocationListener() {

		    @Override
		    public void onLocationChanged(Location location) {
		        // A new location update is received.  Do something useful with it.  In this case,
		        // we're sending the update to a handler which then updates the UI with the new
		        // location.

			    double latitude=0;
			    double longitude=0;
			    latitude = location.getLatitude();
			    longitude = location.getLongitude();
			    
			    try {
				    NetmeraAndroidPush push = new NetmeraAndroidPush();
				    // Set the message you want to send 
				    // to registered devices in your app.
				    push.setMessage("Hello, Android Devices!");
				   
				    push.setCirclePush(new NetmeraGeoLocation(latitude,longitude), 100);
				    
				    // Send the notification and get the result as a Map
				    Map<PushChannel, NetmeraPushDetail> result = push.sendNotification();
				     
				    // Get the push detail of channel and then get the status, error, message etc...
				    NetmeraPushDetail androidDetail = result.get(PushChannel.android);
				    String androidError =androidDetail.getError();
				    String androidStatus = androidDetail.getStatus();
			    }
			    catch (NetmeraException e) {
					// TODO: handle exception
				}
		    }

			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStatusChanged(String provider, int status,
					Bundle extras) {
				// TODO Auto-generated method stub
				
			}
		    
		};

	    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, listener);
		    //Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
	}
	
	public void startGeofence(View view){
		 
		NetmeraGeofence geofence = new NetmeraGeofence();
		geofence.setActivityClass(HelpActivity.class);
		geofence.setSenderId(GlobalVariables.projectId);
		 
		//Below paramaters are optional
		geofence.setIntervalSeconds(10);
		geofence.setIntervalMeters(0);
		geofence.setAllowGPSForLocationTracking(true);
		
		NetmeraGeofenceService.init(geofence, getApplicationContext());
		 
		NetmeraGeofenceService.startGeofenceService();
		
	}

	
	public void stopGeofence(View view){
		 
		NetmeraGeofenceService.stopGeofenceService();
		
	}
}
